package wox.lonice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wox.lonice.dao.WePublicInfoMapper;
import wox.lonice.entity.WePublicInfo;
import wox.lonice.entity.WePublicInfoExample;
import wox.lonice.service.WePublicInfoService;

import java.util.List;

@Service
public class WePublicInfoServiceImpl implements WePublicInfoService {

    @Autowired
    private WePublicInfoMapper wePublicInfoMapper;

    @Override
    public List<WePublicInfo> selectByAll() {
        return wePublicInfoMapper.selectByAll();
    }

    @Override
    public WePublicInfo selectByAlias(String alias) {
        WePublicInfoExample example = new WePublicInfoExample();
        example.createCriteria().andAliasEqualTo(alias);
        List<WePublicInfo> wePublicInfos = wePublicInfoMapper.selectByExample(example);
        if(CollectionUtil.isNotEmpty(wePublicInfos)){
            return wePublicInfos.get(0);
        }
        return null;
    }

    @Override
    public void saveOne(WePublicInfo wePublicInfo) {
        wePublicInfoMapper.insert(wePublicInfo);
    }

    @Override
    public void saveAll(List<WePublicInfo> wePublicInfoList) {
        wePublicInfoList.forEach(wePublicInfo -> {
            WePublicInfo info = this.selectByAlias(wePublicInfo.getAlias());
            if(ObjectUtil.isNotNull(info) && info.equals(wePublicInfo)){
                info.setFakeid(wePublicInfo.getFakeid()).setNickname(wePublicInfo.getNickname())
                        .setRoundHeadImg(wePublicInfo.getRoundHeadImg()).setServiceType(wePublicInfo.getServiceType());
                wePublicInfoMapper.updateByPrimaryKeySelective(info);
            } else {
                wePublicInfoMapper.insert(wePublicInfo);
            }
        });
    }

}
